From f393ed5162532abda62e163df0319a9d8d92de83 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sat, 10 Jun 2023 16:00:37 -0600 Subject: [PATCH] fix humminbird icon matching, (#1126) * fix humminbird icon matching, eliminating one use of xasprintf. This was broken in 6029c8267 over a decade ago. * eliminate unnecessary xasprinf usage. * eliminate another xasprintf usage. * eliminate xasprintf usage. * retire xasprintf, xvasprintf. --- defs.h | 6 --- garmin_txt.cc | 14 +++--- humminbird.cc | 16 ++---- skytraq.cc | 5 +- util.cc | 133 -------------------------------------------------- 5 files changed, 14 insertions(+), 160 deletions(-) diff --git a/defs.h b/defs.h index b4b988993..5794729a7 100644 --- a/defs.h +++ b/defs.h @@ -33,8 +33,6 @@ #include // for QDateTime #include // for QDebug #include // for QList, QList<>::const_iterator, QList<>::const_reverse_iterator, QList<>::count, QList<>::reverse_iterator -#include // for QScopedPointer -#include // for QScopedPointerPodDeleter #include // for QString #include // for QStringView #include // for QTextCodec @@ -1019,10 +1017,6 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n) return s1.left(n).compare(s2.left(n), Qt::CaseInsensitive); } -[[gnu::format(printf, 2, 3)]] int xasprintf(char** strp, const char* fmt, ...); -[[gnu::format(printf, 2, 3)]] int xasprintf(QString* strp, const char* fmt, ...); -[[gnu::format(printf, 2, 3)]] int xasprintf(QScopedPointer& strp, const char* fmt, ...); -[[gnu::format(printf, 2, 0)]] int xvasprintf(char** strp, const char* fmt, va_list ap); char* strupper(char* src); char* strlower(char* src); QDateTime make_datetime(QDate date, QTime time, bool is_localtime, bool force_utc, int utc_offset); diff --git a/garmin_txt.cc b/garmin_txt.cc index c78be4a36..5fd72a870 100644 --- a/garmin_txt.cc +++ b/garmin_txt.cc @@ -81,7 +81,7 @@ static grid_type grid_index; static int datum_index; static const char* datum_str; static int current_line; -static char* date_time_format = nullptr; +static QString date_time_format; static int precision = 3; static time_t utc_offs = 0; static gtxt_flags_t gtxt_flags; @@ -199,7 +199,7 @@ init_date_and_time_format() const char* t = get_option_val(opt_time_format, kDefaultTimeFormat); QString t1 = convert_human_time_format(t); - xasprintf(&date_time_format, "%s %s", CSTR(d1), CSTR(t1)); + date_time_format = QStringLiteral("%1 %2").arg(d1, t1); } static void @@ -384,7 +384,7 @@ print_date_and_time(const time_t time, const bool time_only) } else { tm = *localtime(&time); } - strftime(tbuf, sizeof(tbuf), date_time_format, &tm); + strftime(tbuf, sizeof(tbuf), CSTR(date_time_format), &tm); *fout << QString::asprintf("%s ", tbuf); } *fout << "\t"; @@ -803,7 +803,8 @@ garmin_txt_wr_deinit() fout->close(); delete fout; fout = nullptr; - xfree(date_time_format); + date_time_format.clear(); + date_time_format.squeeze(); } static void @@ -950,7 +951,7 @@ strftime_to_timespec(const char* s) static QDateTime parse_date_and_time(const QString& str) { - QString timespec = strftime_to_timespec(date_time_format); + QString timespec = strftime_to_timespec(CSTR(date_time_format)); return QDateTime::fromString(QString(str).trimmed(), timespec); } @@ -1376,7 +1377,8 @@ garmin_txt_rd_deinit() fin->close(); delete fin; fin = nullptr; - xfree(date_time_format); + date_time_format.clear(); + date_time_format.squeeze(); } static void diff --git a/humminbird.cc b/humminbird.cc index cdf807370..95b4453ed 100644 --- a/humminbird.cc +++ b/humminbird.cc @@ -645,11 +645,7 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt) if (hum.icon == 255) { /* no success, no try to find the item in a more comlex name */ hum.icon = 0; /* i.e. "Diamond" as part of "Diamond, Green" or "Green Diamond" */ for (int i = 0; i < num_icons; i++) { - char* match; - xasprintf(&match, "*%s*", humminbird_icons[i]); - int j = wpt->icon_descr.compare(match, Qt::CaseInsensitive); - xfree(match); - if (j != 0) { + if (wpt->icon_descr.contains(humminbird_icons[i], Qt::CaseInsensitive)) { hum.icon = i; break; } @@ -670,8 +666,8 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt) be_write32(&hum.north, qRound(north)); QString name = (global_opts.synthesize_shortnames) - ? mkshort_from_wpt(wptname_sh, wpt) - : mkshort(wptname_sh, wpt->shortname); + ? mkshort_from_wpt(wptname_sh, wpt) + : mkshort(wptname_sh, wpt->shortname); memset(&hum.name, 0, sizeof(hum.name)); memcpy(&hum.name, CSTR(name), name.length()); @@ -873,10 +869,10 @@ HumminbirdFormat::humminbird_write_rtept(const Waypoint* wpt) const void HumminbirdFormat::humminbird_write_waypoint_wrapper(const Waypoint* wpt) { - char* key; Waypoint* tmpwpt; - xasprintf(&key, "%s\01%.9f\01%.9f", CSTRc(wpt->shortname), wpt->latitude, wpt->longitude); + QString key = QStringLiteral("%1\01%2\01%3").arg(wpt->shortname) + .arg(wpt->latitude, 0, 'f', 9).arg(wpt->longitude, 0, 'f', 9); if (!(tmpwpt = map[key])) { tmpwpt = const_cast(wpt); map[key] = const_cast(wpt); @@ -887,8 +883,6 @@ HumminbirdFormat::humminbird_write_waypoint_wrapper(const Waypoint* wpt) tmpwpt = const_cast(wpt); tmpwpt->extra_data = p; } - - xfree(key); } void diff --git a/skytraq.cc b/skytraq.cc index 239c18bfa..fe4ebb63a 100644 --- a/skytraq.cc +++ b/skytraq.cc @@ -479,7 +479,6 @@ SkytraqBase::skytraq_get_log_buffer_status(uint32_t* log_wr_ptr, uint16_t* secto *sectors_total = le_readu16(&MSG_LOG_STATUS_OUTPUT.sectors_total); // unsigned char log_bool, fifo_mode; - char* mystatus; unsigned int tmax = le_readu32(&MSG_LOG_STATUS_OUTPUT.max_time); unsigned int tmin = le_readu32(&MSG_LOG_STATUS_OUTPUT.min_time); unsigned int dmax = le_readu32(&MSG_LOG_STATUS_OUTPUT.max_dist); @@ -488,9 +487,7 @@ SkytraqBase::skytraq_get_log_buffer_status(uint32_t* log_wr_ptr, uint16_t* secto unsigned int vmin = le_readu32(&MSG_LOG_STATUS_OUTPUT.min_speed); // log_bool = *(MSG_LOG_STATUS_OUTPUT.datalog_enable); // fifo_mode = *(MSG_LOG_STATUS_OUTPUT.log_fifo_mode); - xasprintf(&mystatus, "#logging: tmin=%u, tmax=%u, dmin=%u, dmax=%u, vmin=%u, vmax=%u\n", tmin, tmax, dmin, dmax, vmin, vmax); - db(1, mystatus); - xfree(mystatus); + db(1, "#logging: tmin=%u, tmax=%u, dmin=%u, dmax=%u, vmin=%u, vmax=%u\n", tmin, tmax, dmin, dmax, vmin, vmax); return res_OK; } diff --git a/util.cc b/util.cc index 349549195..1d2328fd1 100644 --- a/util.cc +++ b/util.cc @@ -24,7 +24,6 @@ #include // for errno #include // for INT_MAX, INT_MIN #include // for fabs, floor -#include // for va_list, va_end, va_start, va_copy #include // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout #include // for abs, calloc, free, malloc, realloc #include // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror @@ -35,7 +34,6 @@ #include // for QDateTime #include // for QFileInfo #include // for QList -#include // for QScopedPointer #include // for QString #include // for QTextBoundaryFinder, QTextBoundaryFinder::Grapheme #include // for QTextCodec @@ -185,137 +183,6 @@ ufopen(const QString& fname, const char* mode) #endif } -/* - * Allocate a string using a format list with optional arguments - * Returns -1 on error. - * If return value is anything else, *strp will be populated with an - * allocated string containing the formatted buffer. - * - * Freeing that is the responsibility of the caller. - */ - -int -xasprintf(char** strp, const char* fmt, ...) -{ - va_list args; - - va_start(args, fmt); - int res = xvasprintf(strp, fmt, args); - va_end(args); - - return res; -} - -int -xasprintf(QString* strp, const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - char* cstrp; - int res = xvasprintf(&cstrp, fmt, args); - *strp = cstrp; - xfree(cstrp); - va_end(args); - - return res; -} - -int -xasprintf(QScopedPointer& strp, const char* fmt, ...) -{ - va_list args; - - va_start(args, fmt); - char* cstrp; - int res = xvasprintf(&cstrp, fmt, args); - strp.reset(cstrp); - va_end(args); - - return res; -} - -int -xvasprintf(char** strp, const char* fmt, va_list ap) -{ - /* From http://perfec.to/vsnprintf/pasprintf.c */ - /* size of first buffer malloc; start small to exercise grow routines */ -# define FIRSTSIZE 1 - char* buf = nullptr; - char* newbuf; - size_t nextsize = 0; - int outsize; - va_list args; - - int bufsize = 0; - for (;;) { - if (bufsize == 0) { - if ((buf = (char*) xmalloc(FIRSTSIZE)) == nullptr) { - *strp = nullptr; - return -1; - } - bufsize = FIRSTSIZE; - } else if ((newbuf = (char*) xrealloc(buf, nextsize)) != nullptr) { - buf = newbuf; - bufsize = nextsize; - } else { - xfree(buf); - *strp = nullptr; - return -1; - } - - va_copy(args, ap); - outsize = vsnprintf(buf, bufsize, fmt, args); - va_end(args); - - if (outsize == -1) { - /* Clear indication that output was truncated, but no - * clear indication of how big buffer needs to be, so - * simply double existing buffer size for next time. - */ - nextsize = bufsize * 2; - - } else if (outsize == bufsize) { - /* Output was truncated (since at least the \0 could - * not fit), but no indication of how big the buffer - * needs to be, so just double existing buffer size - * for next time. - */ - nextsize = bufsize * 2; - - } else if (outsize > bufsize) { - /* Output was truncated, but we were told exactly how - * big the buffer needs to be next time. Add two chars - * to the returned size. One for the \0, and one to - * prevent ambiguity in the next case below. - */ - nextsize = outsize + 2; - - } else if (outsize == bufsize - 1) { - /* This is ambiguous. May mean that the output string - * exactly fits, but on some systems the output string - * may have been truncated. We can't tell. - * Just double the buffer size for next time. - */ - nextsize = bufsize * 2; - - } else { - /* Output was not truncated */ - break; - } - } - /* Prevent us from allocating millions of unused bytes. */ - /* O.K.: I think this is not the final solution. */ - if (bufsize > outsize + 1) { - const unsigned ptrsz = sizeof(buf); - if (((bufsize + ptrsz + 1) / ptrsz) > ((outsize + ptrsz + 1) / ptrsz)) { - buf = (char*) xrealloc(buf, outsize + 1); - } - - } - *strp = buf; - return outsize; -} - void printposn(const double c, bool is_lat) { -- 2.30.2